package com.myapp.mobilesafe.activity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EnterPwdActivity extends AppCompatActivity {
    private TextView tvName;
    private ImageView ivIcon;
    private EditText etPwd;
    private Button btnOK;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_pwd);
        tvName = (TextView) findViewById(R.id.tv_name);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnOK = (Button) findViewById(R.id.btn_ok);
        Intent intent = getIntent();
        final String packageName = intent.getStringExtra("packageName");
        PackageManager pm = getPackageManager();
        try {
            ApplicationInfo applicationInfo = pm.getApplicationInfo(
                    packageName, 0);
            String name = applicationInfo.loadLabel(pm).toString();
            Drawable icon = applicationInfo.loadIcon(pm);

            tvName.setText(name);
            ivIcon.setImageDrawable(icon);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String pwd = etPwd.getText().toString().trim();
                if (!TextUtils.isEmpty(pwd)) {
                    if (pwd.equals("123")) {
                        // 通知看门狗,跳过当前包名的验证
                        Intent intent = new Intent();
                        intent.setAction("com.myapp.mobilesafe.SKIP_CHECK");
                        intent.putExtra("package", packageName);//传递包名
                        sendBroadcast(intent);

                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(),"密码错误,密码是123哦!",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(),"内容不能为空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    public void onBackPressed() {
        // 跳到桌面
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);

        finish();
    }
}
